package com.luckmgr.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.common.entity.PageBean;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.LuckRecord;
import com.luckmgr.entity.db.Prize;
import com.luckmgr.service.ActivityService;
import com.luckmgr.service.LuckRecordService;
import com.luckmgr.service.PrizeService;

@Controller
@RequestMapping("/lkrecord")
public class LuckRecordController {
	
	@Autowired
	PrizeService prizeServiceImpl;
	@Autowired
	ActivityService activityServiceImpl;
	
	@Autowired
	LuckRecordService luckRecordServiceImpl;
	
	@RequestMapping(value = "")
	public String main(Model model,HttpSession session,Integer aid,Integer pid,String name,Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("aid", aid);
		map.put("pid", pid);
		map.put("name", name);
		PageBean<LuckRecord> pagebean = luckRecordServiceImpl.selectListPage(map, page);
		List<Activity> activitys = activityServiceImpl.selectList(null);
		if(aid!=null){
			Map<String,Object> pmap = new HashMap<String, Object>();
			map.put("aid", aid);
			List<Prize> prizes = prizeServiceImpl.selectList(pmap);
			model.addAttribute("prizes", prizes);
		}
		
		
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("activitys", activitys);
		model.addAttribute("aid", aid);
		model.addAttribute("pid", pid);
		model.addAttribute("name", name);
		return "lkrecord/index";
	}
	
	@RequestMapping(value = "upstatus")
	public String upstatus(Model model,final RedirectAttributes redirectAttributes,Integer aid,Integer pid,String name,Integer rid) {
		if(null ==rid){
			model.addAttribute("msg", "活动已开始，不能进行此操作!");
			return "actlevel/addup";
		}
		LuckRecord luckRecord = luckRecordServiceImpl.selectByPrimaryKey(rid);
		if(null != luckRecord){
			if(luckRecord.getStatus()==0){
				luckRecord.setStatus(1);
			}else{
				luckRecord.setStatus(0);
			}
			luckRecordServiceImpl.updateEntity(luckRecord);
		}
		
		redirectAttributes.addAttribute("aid", aid);
		redirectAttributes.addAttribute("pid", pid);
		redirectAttributes.addAttribute("name", name);
		return "redirect:/lkrecord";
	}
	

}
